$(document).ready(function(){
    
    /*Datatables*/
    $('table.pretty').dataTable({
                    "oLanguage": {
                        "sProcessing":   "Đang xử lý...",
                        "sLengthMenu":   "Hiển thị _MENU_ kết quả",
                        "sZeroRecords":  "Không tìm thấy dòng nào phù hợp",
                        "sInfo":         "Đang xem _START_ đến _END_ trong tổng số _TOTAL_ kết quả",
                        "sInfoEmpty":    "Đang xem 0 đến 0 trong tổng số 0 kết quả",
                        "sInfoFiltered": "(được lọc từ _MAX_ mục)",
                        "sInfoPostFix":  "",
                        "sSearch":       "Tìm:",
                        "sUrl":          "",
                        "oPaginate": {
                            "sFirst":    "Đầu",
                            "sPrevious": "Trước",
                            "sNext":     "Tiếp",
                            "sLast":     "Cuối"
                        }
                    },
                    "sPaginationType":"full_numbers",
                    "bJQueryUI":false,
                    "aaSorting": [[ 0, "desc" ]],
                    "iDisplayLength": 50
                });
    /*End of Datatables script*/
    
    /*Jcrop*/
    
    function doCoords(c)
	{
		jQuery('#x').val(c.x);
    	jQuery('#y').val(c.y);
    	jQuery('#x2').val(c.x2);
    	jQuery('#y2').val(c.y2);
    	jQuery('#w').val(c.w);
    	jQuery('#h').val(c.h);
	};

	function showCoords(c)
    {
    	$('#x').val(c.x);
    	$('#y').val(c.y);
    	$('#x2').val(c.x2);
    	$('#y2').val(c.y2);
    	$('#w').val(c.w);
    	$('#h').val(c.h);
    };
    /*End Jcrop script*/
    
    /*Uploadify*/
    var html = '';
    var base_url = 'http://localhost/my_framework/admin/';
    var folder_image = 'assets/uploads/images/';
    var jcrop_api;
    
    $("#link_upload").uploadify({
				'uploader'		: base_url + 'assets/backend/uploadify/uploadify.swf',
				'script'		: base_url + 'backend/cms/uploadify',
				'cancelImg'		: base_url + 'assets/backend/uploadify/cancel.png',
				'folder'		: '../../../../' + folder_image + 'link/',
				'fileDesc'		: 'Files Ảnh',
				'fileExt'		: '*.jpg;*.jpeg;*.gif;*.png',
				'sizeLimit'		: 100 * 1024 * 1024,
				'multi'			: false,	
				'auto'			: true,
                'buttonText'    : 'BROWSE',                
				'onError'		: function(a, b, c, d){
					if(d.status=404)
						alert('Lỗi upload.');
					else if(d.type === "HTTP")
						alert('error'+d.type+": "+d.info);
					else if(d.type === "File Size")
						alert(c.name+' '+d.type+' Lớn hơn kích thước tối đa: '+Math.round(d.sizeLimit/1024)+'KB');
					else
						alert('error'+d.type+": "+d.text);
				},
				'onComplete'	: function(event, ID, fileObj, response, data){
						if(response == 1)
                        {
                            alert('Ảnh không được nhỏ hơn 100x50');
                        }
                        else
                        {
                            var strJSON = $.parseJSON(response);
                            //alert(strJSON);
                            
                            html += '<div id="gray">';
                            html += '</div>';
                            html += '<div id=\"image_lb\">';
                            html += '<form method=\"POST\" action=\"' + base_url + 'backend/cms/processimg\" >';
                            html += '<img id=\"cropbox\" width = ' + strJSON['width'] + ' height = ' + strJSON['height'] + ' src=\"' + base_url + folder_image + 'link/' +  strJSON['file_name'] + '\">';
                            html += "<input type=\"hidden\" id=\"x\" name=\"x\" value=\"\" />";
                            html += '<input type=\"hidden\" value=\"\" name=\"y\" id=\"y\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"x2\" id=\"x2\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"y2\" id=\"y2\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"w\" id=\"w\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"h\" id=\"h\" />';
                            html += '<input type=\"hidden\" value=\"' + strJSON['file_name'] + '\" name=\"filename\" id=\"filename\" />';
                            html += '<input type=\"hidden\" name=\"filepath\" id=\"filepath\" value=\"' + strJSON['fullpath'] + '\" />';
                            html += '<input type=\"submit\" class=\"sub\"  value=\"Cắt\" id=\"jcrop\" name=\"jcrop\" />';
                            html += '<input type=\"submit\" class=\"sub\" value=\"Thoát\" id=\"cancel\" name=\"cancel\" />';
                            html += '</form>';
                            html += '</div>';
                            
                            //alert(strJSON['fullpath']);
                            $('#main-content').append(html);
                            
                            /*JCrop*/
                            $("#cropbox").Jcrop({
                                onSelect: doCoords,
                                onChange: doCoords,
                                minSize : [107,63],
                                setSelect: [0, 0, 107, 63]
                            },function(){
                                jcrop_api = this;
                            });
                            
                            resize_poll(strJSON['width'], strJSON['height']);
                        }
                        
				}
			});
    /*End of Uploadify script*/
    
    /*Center*/
    var wei = $(window).width();
   
    var hei = $(window).height();
    
    function resize_poll(wei_object,hei_object)
    {
        $('#image_lb').css({'left': (wei-wei_object)/2, 'top': (hei-hei_object)/2}); 
    }
    
    /*Hide lightbox*/
    $('#gray').live('click',function(eve){
        
        eve.preventDefault();
        
        $('#gray').fadeOut("fast",function(){
            $('#image_lb').hide();
        });
         
   });
    /*End of Center script*/
    
    /*Proccess Image*/
    $('#jcrop').live('click', function(e){
        e.preventDefault();
        
        var x   = jQuery('#x').attr('value');
        var y   = jQuery('#y').attr('value');
        var x2  = jQuery('#x2').attr('value');
        var y2  = jQuery('#y2').attr('value');
        var w   = jQuery('#w').attr('value');
        var h   = jQuery('#h').attr('value');
        var filename   = jQuery('#filename').attr('value');
        //var imagepath = jQuery('#imagepath').attr('value');
        var filepath = jQuery('#filepath').attr('value');
        var link = base_url + 'backend/cms/processimg';
        
        $.ajax({
               url: link,
               type: "POST",
               data: "x1="+x+"&y1="+y+"&x2="+x2+"&y2="+y2+"&w="+w+"&h="+h+"&filepath="+filepath,
               success: function(result){
                   var strJSON = $.parseJSON(result);
                   
                   var value_thumbs = folder_image + 'link/thumbs/' + strJSON['img_name'];
                   var value_image = folder_image + 'link/' + filename;
                   
                   $('#link_upload').attr('value',value_thumbs);
                   $('#path_img_link').attr('value',value_image);
                   
                   $('#thumbs').html("<img width=\'340px\' style=\'border: 2px solid #ccc\' height=\'160px\' src=\'" + base_url + folder_image + "link/thumbs/" + strJSON['img_name'] +"\' />");
                   $('#gray').fadeOut("fast",function(){
                        $('#image_lb').hide();
                   });                     
               }         
        });
    });
    
    $('#cancel').live('click', function(e){
        e.preventDefault();
        
        var filepath = jQuery('#filepath').attr('value');
        var link = base_url + 'backend/cms/rejectimg';
        
        $.ajax({
            url:   link,
            type:  "POST",
            data:  "filepath="+filepath,
            success: function(result){
                 $('#gray').fadeOut("fast",function(){
                    $('#image_lb').hide();
                 window.location.reload();
                }); 
            }
        });
    });
    
    /*End processimage script*/                        
    
    /*****************************Uploadify for News**************************************/
    $("#news_image").uploadify({
				'uploader'		: base_url + 'assets/backend/uploadify/uploadify.swf',
				'script'		: base_url + 'backend/news/uploadify',
				'cancelImg'		: base_url + 'assets/backend/uploadify/cancel.png',
				'folder'		: '../../../../' + folder_image + 'news/',
				'fileDesc'		: 'Files Ảnh',
				'fileExt'		: '*.jpg;*.jpeg;*.gif;*.png',
				'sizeLimit'		: 100 * 1024 * 1024,
				'multi'			: false,	
				'auto'			: true,
                'buttonText'    : 'BROWSE',                
				'onError'		: function(a, b, c, d){
					if(d.status=404)
						alert('Lỗi upload.');
					else if(d.type === "HTTP")
						alert('error'+d.type+": "+d.info);
					else if(d.type === "File Size")
						alert(c.name+' '+d.type+' Lớn hơn kích thước tối đa: '+Math.round(d.sizeLimit/1024)+'KB');
					else
						alert('error'+d.type+": "+d.text);
				},
				'onComplete'	: function(event, ID, fileObj, response, data){
						if(response == 1)
                        {
                            alert('Ảnh không được nhỏ hơn 150x150');
                        }
                        else
                        {
                            var strJSON = $.parseJSON(response);
                            
                            html += '<div id="gray">';
                            html += '</div>';
                            html += '<div id=\"image_lb\">';
                            html += '<form method=\"POST\" action=\"' + base_url + 'backend/news/processimg\" >';
                            html += '<img id=\"cropbox\" width = ' + strJSON['width'] + ' height = ' + strJSON['height'] + ' src=\"' + base_url + folder_image + 'news/' +  strJSON['file_name'] + '\">';
                            html += "<input type=\"hidden\" id=\"x\" name=\"x\" value=\"\" />";
                            html += '<input type=\"hidden\" value=\"\" name=\"y\" id=\"y\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"x2\" id=\"x2\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"y2\" id=\"y2\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"w\" id=\"w\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"h\" id=\"h\" />';
                            html += '<input type=\"hidden\" value=\"' + strJSON['file_name'] + '\" name=\"filename\" id=\"filename\" />';
                            html += '<input type=\"hidden\" name=\"filepath\" id=\"filepath\" value=\"' + strJSON['fullpath'] + '\" />';
                            html += '<input type=\"submit\" class=\"sub\"  value=\"Cắt\" id=\"newsjcrop\" name=\"newsjcrop\" />';
                            html += '<input type=\"submit\" class=\"sub\" value=\"Thoát\" id=\"newscancel\" name=\"newscancel\" />';
                            html += '</form>';
                            html += '</div>';
                            
                            //alert(strJSON['fullpath']);
                            $('#main-content').append(html);
                            
                            /*JCrop*/
                            $("#cropbox").Jcrop({
                                onSelect: doCoords,
                                onChange: doCoords,
                                minSize : [150,150],
                                setSelect: [0, 0, 150,150]
                            },function(){
                                jcrop_api = this;
                            });
                            
                            resize_poll(strJSON['width'], strJSON['height']);
                        }
                        
				}
			});
            
    /*Edit*/
    $("#news_image_edit").uploadify({
				'uploader'		: base_url + 'assets/backend/uploadify/uploadify.swf',
				'script'		: base_url + 'backend/news/uploadify',
				'cancelImg'		: base_url + 'assets/backend/uploadify/cancel.png',
				'folder'		: '../../../../../' + folder_image + 'news/',
				'fileDesc'		: 'Files Ảnh',
				'fileExt'		: '*.jpg;*.jpeg;*.gif;*.png',
				'sizeLimit'		: 100 * 1024 * 1024,
				'multi'			: false,	
				'auto'			: true,
                'buttonText'    : 'BROWSE',                
				'onError'		: function(a, b, c, d){
					if(d.status=404)
						alert('Lỗi upload.');
					else if(d.type === "HTTP")
						alert('error'+d.type+": "+d.info);
					else if(d.type === "File Size")
						alert(c.name+' '+d.type+' Lớn hơn kích thước tối đa: '+Math.round(d.sizeLimit/1024)+'KB');
					else
						alert('error'+d.type+": "+d.text);
				},
				'onComplete'	: function(event, ID, fileObj, response, data){
						if(response == 1)
                        {
                            alert('Ảnh không được nhỏ hơn 150x150');
                        }
                        else
                        {
                            var strJSON = $.parseJSON(response);
                            //alert(strJSON);
                            
                            html += '<div id="gray">';
                            html += '</div>';
                            html += '<div id=\"image_lb\">';
                            html += '<form method=\"POST\" action=\"' + base_url + 'backend/news/processimg\" >';
                            html += '<img id=\"cropbox\" width = ' + strJSON['width'] + ' height = ' + strJSON['height'] + ' src=\"' + base_url + folder_image + 'news/' +  strJSON['file_name'] + '\">';
                            html += "<input type=\"hidden\" id=\"x\" name=\"x\" value=\"\" />";
                            html += '<input type=\"hidden\" value=\"\" name=\"y\" id=\"y\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"x2\" id=\"x2\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"y2\" id=\"y2\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"w\" id=\"w\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"h\" id=\"h\" />';
                            html += '<input type=\"hidden\" value=\"' + strJSON['file_name'] + '\" name=\"filename\" id=\"filename\" />';
                            html += '<input type=\"hidden\" name=\"filepath\" id=\"filepath\" value=\"' + strJSON['fullpath'] + '\" />';
                            html += '<input type=\"submit\" class=\"sub\"  value=\"Cắt\" id=\"newsjcropedit\" name=\"newsjcrop\" />';
                            html += '<input type=\"submit\" class=\"sub\" value=\"Thoát\" id=\"newscancel\" name=\"newscancel\" />';
                            html += '</form>';
                            html += '</div>';
                            
                            //alert(strJSON['fullpath']);
                            $('#main-content').append(html);
                            
                            /*JCrop*/
                            $("#cropbox").Jcrop({
                                onSelect: doCoords,
                                onChange: doCoords,
                                minSize : [150,150],
                                setSelect: [0, 0, 150,150]
                            },function(){
                                jcrop_api = this;
                            });
                            
                            resize_poll(strJSON['width'], strJSON['height']);
                        }
                        
                }
			});
                        
    /*Proccess Image*/
    $('#newsjcrop').live('click', function(e){
        e.preventDefault();
        
        var x   = jQuery('#x').attr('value');
        var y   = jQuery('#y').attr('value');
        var x2  = jQuery('#x2').attr('value');
        var y2  = jQuery('#y2').attr('value');
        var w   = jQuery('#w').attr('value');
        var h   = jQuery('#h').attr('value');
        var filename   = jQuery('#filename').attr('value');
        //var imagepath = jQuery('#imagepath').attr('value');
        var filepath = jQuery('#filepath').attr('value');
        var link = base_url + 'backend/news/processimg';
        
        $.ajax({
               url: link,
               type: "POST",
               data: "x1="+x+"&y1="+y+"&x2="+x2+"&y2="+y2+"&w="+w+"&h="+h+"&filepath="+filepath,
               success: function(result){
                   var strJSON = $.parseJSON(result);
                   
                   var value_thumbs = folder_image + 'news/thumbs/' + strJSON['img_name'];
                   var value_image = folder_image + 'news/' + filename;
                   
                   $('#news_image_thumb').attr('value',value_thumbs);
                   $('#news_image').attr('value',value_image);
                   
                   $('#newsthumbs').html("<img style=\'border: 2px solid #ccc\'  src=\'" + base_url + folder_image + "news/thumbs/" + strJSON['img_name'] +"\' />");
                   $('#gray').fadeOut("fast",function(){
                        $('#image_lb').hide();
                   });                     
               }         
        });
    });
    
    $('#newsjcropedit').live('click', function(e){
        e.preventDefault();
        
        var x   = jQuery('#x').attr('value');
        var y   = jQuery('#y').attr('value');
        var x2  = jQuery('#x2').attr('value');
        var y2  = jQuery('#y2').attr('value');
        var w   = jQuery('#w').attr('value');
        var h   = jQuery('#h').attr('value');
        var filename   = jQuery('#filename').attr('value');
        //var imagepath = jQuery('#imagepath').attr('value');
        var filepath = jQuery('#filepath').attr('value');
        var link = base_url + 'backend/news/processimg';
        
        $.ajax({
               url: link,
               type: "POST",
               data: "x1="+x+"&y1="+y+"&x2="+x2+"&y2="+y2+"&w="+w+"&h="+h+"&filepath="+filepath,
               success: function(result){
                   var strJSON = $.parseJSON(result);
                   
                   var value_thumbs = folder_image + 'news/thumbs/' + strJSON['img_name'];
                   var value_image = folder_image + 'news/' + filename;
                   
                   $('#news_image_thumb_edit').attr('value',value_thumbs);
                   $('#news_image_edit').attr('value',value_image);
                   
                   $('#newsthumbs').empty();
                   $('#newsthumbs').html("<img  style=\'border: 2px solid #ccc\' src=\'" + base_url + folder_image + "news/thumbs/" + strJSON['img_name'] +"\' />");
                   $('#gray').fadeOut("fast",function(){
                        $('#image_lb').hide();
                   });                     
               }         
        });
    });
    /*Event Cancel*/
    $('#newscancel').live('click', function(e){
        e.preventDefault();
        
        var filepath = jQuery('#filepath').attr('value');
        var link = base_url + 'backend/news/rejectimg';
        
        $.ajax({
            url:   link,
            type:  "POST",
            data:  "filepath="+filepath,
            success: function(result){
                 $('#gray').fadeOut("fast",function(){
                    $('#image_lb').hide();
                 window.location.reload();
                }); 
            }
        });
    });
    /****************************End Uploadify for News **********************************/
    
    /********************************Uploadify for Slide**********************************/
    $("#slide_image").uploadify({
				'uploader'		: base_url + 'assets/backend/uploadify/uploadify.swf',
				'script'		: base_url + 'backend/slide/uploadify',
				'cancelImg'		: base_url + 'assets/backend/uploadify/cancel.png',
				'folder'		: '../../../../' + folder_image + 'slides/',
				'fileDesc'		: 'Files Ảnh',
				'fileExt'		: '*.jpg;*.jpeg;*.gif;*.png',
				'sizeLimit'		: 100 * 1024 * 1024,
				'multi'			: false,	
				'auto'			: true,
                'buttonText'    : 'BROWSE',                
				'onError'		: function(a, b, c, d){
					if(d.status=404)
						alert('Lỗi upload.');
					else if(d.type === "HTTP")
						alert('error'+d.type+": "+d.info);
					else if(d.type === "File Size")
						alert(c.name+' '+d.type+' Lớn hơn kích thước tối đa: '+Math.round(d.sizeLimit/1024)+'KB');
					else
						alert('error'+d.type+": "+d.text);
				},
				'onComplete'	: function(event, ID, fileObj, response, data){
						if(response == 1)
                        {
                            alert('Ảnh không được nhỏ hơn 470x353');
                        }
                        else
                        {
                            var strJSON = $.parseJSON(response);
                            //alert(strJSON);
                            
                            html += '<div id="gray">';
                            html += '</div>';
                            html += '<div id=\"image_lb\">';
                            html += '<form method=\"POST\" action=\"' + base_url + 'backend/slide/processimg\" >';
                            html += '<img id=\"cropbox\" width = ' + strJSON['width'] + ' height = ' + strJSON['height'] + ' src=\"' + base_url + folder_image + 'slides/' +  strJSON['file_name'] + '\">';
                            html += "<input type=\"hidden\" id=\"x\" name=\"x\" value=\"\" />";
                            html += '<input type=\"hidden\" value=\"\" name=\"y\" id=\"y\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"x2\" id=\"x2\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"y2\" id=\"y2\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"w\" id=\"w\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"h\" id=\"h\" />';
                            html += '<input type=\"hidden\" value=\"' + strJSON['file_name'] + '\" name=\"filename\" id=\"filename\" />';
                            html += '<input type=\"hidden\" name=\"filepath\" id=\"filepath\" value=\"' + strJSON['fullpath'] + '\" />';
                            html += '<input type=\"submit\" class=\"sub\"  value=\"Cắt\" id=\"slidejcrop\" name=\"slidejcrop\" />';
                            html += '<input type=\"submit\" class=\"sub\" value=\"Thoát\" id=\"slidecancel\" name=\"slidecancel\" />';
                            html += '</form>';
                            html += '</div>';
                            
                            //alert(strJSON['fullpath']);
                            $('#main-content').append(html);
                            
                            /*JCrop*/
                            $("#cropbox").Jcrop({
                                onSelect: doCoords,
                                onChange: doCoords,
                                minSize : [470,353],
                                setSelect: [0, 0, 470,353]
                            },function(){
                                jcrop_api = this;
                            });
                            
                            resize_poll(strJSON['width'], strJSON['height']);
                        }
                        
				}
			});
        
            $("#slide_image_edit").uploadify({
				'uploader'		: base_url + 'assets/backend/uploadify/uploadify.swf',
				'script'		: base_url + 'backend/slide/uploadify',
				'cancelImg'		: base_url + 'assets/backend/uploadify/cancel.png',
				'folder'		: '../../../../../' + folder_image + 'slides/',
				'fileDesc'		: 'Files Ảnh',
				'fileExt'		: '*.jpg;*.jpeg;*.gif;*.png',
				'sizeLimit'		: 100 * 1024 * 1024,
				'multi'			: false,	
				'auto'			: true,
                'buttonText'    : 'BROWSE',                
				'onError'		: function(a, b, c, d){
					if(d.status=404)
						alert('Lỗi upload.');
					else if(d.type === "HTTP")
						alert('error'+d.type+": "+d.info);
					else if(d.type === "File Size")
						alert(c.name+' '+d.type+' Lớn hơn kích thước tối đa: '+Math.round(d.sizeLimit/1024)+'KB');
					else
						alert('error'+d.type+": "+d.text);
				},
				'onComplete'	: function(event, ID, fileObj, response, data){
						if(response == 1)
                        {
                            alert('Ảnh không được nhỏ hơn 470x353');
                        }
                        else
                        {
                            var strJSON = $.parseJSON(response);
                            //alert(strJSON);
                            
                            html += '<div id="gray">';
                            html += '</div>';
                            html += '<div id=\"image_lb\">';
                            html += '<form method=\"POST\" action=\"' + base_url + 'backend/slide/processimg\" >';
                            html += '<img id=\"cropbox\" width = ' + strJSON['width'] + ' height = ' + strJSON['height'] + ' src=\"' + base_url + folder_image + 'slides/' +  strJSON['file_name'] + '\">';
                            html += "<input type=\"hidden\" id=\"x\" name=\"x\" value=\"\" />";
                            html += '<input type=\"hidden\" value=\"\" name=\"y\" id=\"y\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"x2\" id=\"x2\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"y2\" id=\"y2\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"w\" id=\"w\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"h\" id=\"h\" />';
                            html += '<input type=\"hidden\" value=\"' + strJSON['file_name'] + '\" name=\"filename\" id=\"filename\" />';
                            html += '<input type=\"hidden\" name=\"filepath\" id=\"filepath\" value=\"' + strJSON['fullpath'] + '\" />';
                            html += '<input type=\"submit\" class=\"sub\"  value=\"Cắt\" id=\"slidejcropedit\" name=\"slidejcropedit\" />';
                            html += '<input type=\"submit\" class=\"sub\" value=\"Thoát\" id=\"slidecancel\" name=\"slidecancel\" />';
                            html += '</form>';
                            html += '</div>';
                            
                            //alert(strJSON['fullpath']);
                            $('#main-content').append(html);
                            
                            /*JCrop*/
                            $("#cropbox").Jcrop({
                                onSelect: doCoords,
                                onChange: doCoords,
                                minSize : [470,353],
                                setSelect: [0, 0, 470,353]
                            },function(){
                                jcrop_api = this;
                            });
                            
                            resize_poll(strJSON['width'], strJSON['height']);
                        }
                        
				}
			});
        /**********************************************************************************/
        $('#slidejcrop').live('click', function(e){
            e.preventDefault();
            
            var x   = jQuery('#x').attr('value');
            var y   = jQuery('#y').attr('value');
            var x2  = jQuery('#x2').attr('value');
            var y2  = jQuery('#y2').attr('value');
            var w   = jQuery('#w').attr('value');
            var h   = jQuery('#h').attr('value');
            var filename   = jQuery('#filename').attr('value');
            //var imagepath = jQuery('#imagepath').attr('value');
            var filepath = jQuery('#filepath').attr('value');
            var link = base_url + 'backend/slide/processimg';
            
            $.ajax({
                   url: link,
                   type: "POST",
                   data: "x1="+x+"&y1="+y+"&x2="+x2+"&y2="+y2+"&w="+w+"&h="+h+"&filepath="+filepath,
                   success: function(result){
                       var strJSON = $.parseJSON(result);
                       
                       var value_thumbs = folder_image + 'slides/thumbs/' + strJSON['img_name'];
                       var value_image = folder_image + 'slides/' + filename;
                       
                       $('#slide_image_thumb').attr('value',value_thumbs);
                       $('#slide_image').attr('value',value_image);
                       
                       $('#slidethumbs').html("<img style=\'border: 2px solid #ccc\' src=\'" + base_url + folder_image + "slides/thumbs/" + strJSON['img_name'] +"\' />");
                       $('#gray').fadeOut("fast",function(){
                            $('#image_lb').hide();
                       });                     
                   }         
            });
        });
        
           $('#slidejcropedit').live('click', function(e){
            e.preventDefault();
            
            var x   = jQuery('#x').attr('value');
            var y   = jQuery('#y').attr('value');
            var x2  = jQuery('#x2').attr('value');
            var y2  = jQuery('#y2').attr('value');
            var w   = jQuery('#w').attr('value');
            var h   = jQuery('#h').attr('value');
            var filename   = jQuery('#filename').attr('value');
            //var imagepath = jQuery('#imagepath').attr('value');
            var filepath = jQuery('#filepath').attr('value');
            var link = base_url + 'backend/slide/processimg';
            
            $.ajax({
                   url: link,
                   type: "POST",
                   data: "x1="+x+"&y1="+y+"&x2="+x2+"&y2="+y2+"&w="+w+"&h="+h+"&filepath="+filepath,
                   success: function(result){
                       var strJSON = $.parseJSON(result);
                       
                       var value_thumbs = folder_image + 'slides/thumbs/' + strJSON['img_name'];
                       var value_image = folder_image + 'slides/' + filename;
                       
                       $('#slide_image_thumb_edit').attr('value',value_thumbs);
                       $('#slide_image_edit').attr('value',value_image);
                       
                       $('#slidethumbsedit').empty();
                       $('#slidethumbsedit').html("<img style=\'border: 2px solid #ccc\' src=\'" + base_url + folder_image + "slides/thumbs/" + strJSON['img_name'] +"\' />");
                       $('#gray').fadeOut("fast",function(){
                            $('#image_lb').hide();
                       });                     
                   }         
            });
        });
        
    /**************************************************************/
    /*Event Cancel*/
    $('#slidecancel').live('click', function(e){
        e.preventDefault();
        
        var filepath = jQuery('#filepath').attr('value');
        var link = base_url + 'backend/slide/rejectimg';
        
        $.ajax({
            url:   link,
            type:  "POST",
            data:  "filepath="+filepath,
            success: function(result){
                 $('#gray').fadeOut("fast",function(){
                    $('#image_lb').hide();
                    window.location.reload();
                }); 
            }
        });
    });
    
    /********************************End Uploadify for Slide******************************/
    
    /********************************Uploadify for Product******************************/
      $("#product_img").uploadify({
				'uploader'		: base_url + 'assets/backend/uploadify/uploadify.swf',
				'script'		: base_url + 'backend/product/uploadify',
				'cancelImg'		: base_url + 'assets/backend/uploadify/cancel.png',
				'folder'		: '../../../../' + folder_image + 'product/',
				'fileDesc'		: 'Files Ảnh',
				'fileExt'		: '*.jpg;*.jpeg;*.gif;*.png',
				'sizeLimit'		: 100 * 1024 * 1024,
				'multi'			: false,	
				'auto'			: true,
                'buttonText'    : 'BROWSE',                
				'onError'		: function(a, b, c, d){
					if(d.status=404)
						alert('Lỗi upload.');
					else if(d.type === "HTTP")
						alert('error'+d.type+": "+d.info);
					else if(d.type === "File Size")
						alert(c.name+' '+d.type+' Lớn hơn kích thước tối đa: '+Math.round(d.sizeLimit/1024)+'KB');
					else
						alert('error'+d.type+": "+d.text);
				},
				'onComplete'	: function(event, ID, fileObj, response, data){
						if(response == 1)
                        {
                            alert('Ảnh không được nhỏ hơn 150x150');
                        }
                        else
                        {
                            var strJSON = $.parseJSON(response);
                            //alert(strJSON);
                            
                            html += '<div id="gray">';
                            html += '</div>';
                            html += '<div id=\"image_lb\">';
                            html += '<form method=\"POST\" action=\"' + base_url + 'backend/product/processimg\" >';
                            html += '<img id=\"cropbox\" width = ' + strJSON['width'] + ' height = ' + strJSON['height'] + ' src=\"' + base_url + folder_image + 'product/' +  strJSON['file_name'] + '\">';
                            html += "<input type=\"hidden\" id=\"x\" name=\"x\" value=\"\" />";
                            html += '<input type=\"hidden\" value=\"\" name=\"y\" id=\"y\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"x2\" id=\"x2\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"y2\" id=\"y2\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"w\" id=\"w\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"h\" id=\"h\" />';
                            html += '<input type=\"hidden\" value=\"' + strJSON['file_name'] + '\" name=\"filename\" id=\"filename\" />';
                            html += '<input type=\"hidden\" name=\"filepath\" id=\"filepath\" value=\"' + strJSON['fullpath'] + '\" />';
                            html += '<input type=\"submit\" class=\"sub\"  value=\"Cắt\" id=\"productjcrop\" name=\"productjcrop\" />';
                            html += '<input type=\"submit\" class=\"sub\" value=\"Thoát\" id=\"productcancel\" name=\"productcancel\" />';
                            html += '</form>';
                            html += '</div>';
                            
                            //alert(strJSON['fullpath']);
                            $('#main-content').append(html);
                            
                            /*JCrop*/
                            $("#cropbox").Jcrop({
                                onSelect: doCoords,
                                onChange: doCoords,
                                minSize : [100, 50],
                                setSelect: [0, 0, 100,50]
                            },function(){
                                jcrop_api = this;
                            });
                            
                            resize_poll(strJSON['width'], strJSON['height']);
                        }
                        
				}
			});
    
          $("#product_img_edit").uploadify({
				'uploader'		: base_url + 'assets/backend/uploadify/uploadify.swf',
				'script'		: base_url + 'backend/product/uploadify',
				'cancelImg'		: base_url + 'assets/backend/uploadify/cancel.png',
				'folder'		: '../../../../../' + folder_image + 'product/',
				'fileDesc'		: 'Files Ảnh',
				'fileExt'		: '*.jpg;*.jpeg;*.gif;*.png',
				'sizeLimit'		: 100 * 1024 * 1024,
				'multi'			: false,	
				'auto'			: true,
                'buttonText'    : 'BROWSE',                
				'onError'		: function(a, b, c, d){
					if(d.status=404)
						alert('Lỗi upload.');
					else if(d.type === "HTTP")
						alert('error'+d.type+": "+d.info);
					else if(d.type === "File Size")
						alert(c.name+' '+d.type+' Lớn hơn kích thước tối đa: '+Math.round(d.sizeLimit/1024)+'KB');
					else
						alert('error'+d.type+": "+d.text);
				},
				'onComplete'	: function(event, ID, fileObj, response, data){
						if(response == 1)
                        {
                            alert('Ảnh không được nhỏ hơn 150x150');
                        }
                        else
                        {
                            var strJSON = $.parseJSON(response);
                            //alert(strJSON);
                            
                            html += '<div id="gray">';
                            html += '</div>';
                            html += '<div id=\"image_lb\">';
                            html += '<form method=\"POST\" action=\"' + base_url + 'backend/product/processimg\" >';
                            html += '<img id=\"cropbox\" width = ' + strJSON['width'] + ' height = ' + strJSON['height'] + ' src=\"' + base_url + folder_image + 'product/' +  strJSON['file_name'] + '\">';
                            html += "<input type=\"hidden\" id=\"x\" name=\"x\" value=\"\" />";
                            html += '<input type=\"hidden\" value=\"\" name=\"y\" id=\"y\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"x2\" id=\"x2\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"y2\" id=\"y2\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"w\" id=\"w\" />';
                            html += '<input type=\"hidden\" value=\"\" name=\"h\" id=\"h\" />';
                            html += '<input type=\"hidden\" value=\"' + strJSON['file_name'] + '\" name=\"filename\" id=\"filename\" />';
                            html += '<input type=\"hidden\" name=\"filepath\" id=\"filepath\" value=\"' + strJSON['fullpath'] + '\" />';
                            html += '<input type=\"submit\" class=\"sub\"  value=\"Cắt\" id=\"productjcropedit\" name=\"productjcropedit\" />';
                            html += '<input type=\"submit\" class=\"sub\" value=\"Thoát\" id=\"productcancel\" name=\"productcancel\" />';
                            html += '</form>';
                            html += '</div>';
                            
                            //alert(strJSON['fullpath']);
                            $('#main-content').append(html);
                            
                            /*JCrop*/
                            $("#cropbox").Jcrop({
                                onSelect: doCoords,
                                onChange: doCoords,
                                minSize : [100, 50],
                                setSelect: [0, 0, 100,50]
                            },function(){
                                jcrop_api = this;
                            });
                            
                            resize_poll(strJSON['width'], strJSON['height']);
                        }
                        
				}
			});
    
    /*Event Crop*/
    $('#productjcrop').live('click', function(e){
            e.preventDefault();
            
            var x   = jQuery('#x').attr('value');
            var y   = jQuery('#y').attr('value');
            var x2  = jQuery('#x2').attr('value');
            var y2  = jQuery('#y2').attr('value');
            var w   = jQuery('#w').attr('value');
            var h   = jQuery('#h').attr('value');
            var filename   = jQuery('#filename').attr('value');
            //var imagepath = jQuery('#imagepath').attr('value');
            var filepath = jQuery('#filepath').attr('value');
            var link = base_url + 'backend/product/processimg';
            
            $.ajax({
                   url: link,
                   type: "POST",
                   data: "x1="+x+"&y1="+y+"&x2="+x2+"&y2="+y2+"&w="+w+"&h="+h+"&filepath="+filepath,
                   success: function(result){
                       var strJSON = $.parseJSON(result);
                       
                       var value_thumbs = folder_image + 'product/thumbs/' + strJSON['img_name'];
                       var value_image = folder_image + 'product/' + filename;
                       
                       $('#product_thumb').attr('value',value_thumbs);
                       $('#product_img').attr('value',value_image);
                       
                       $('#productthumbs').html("<img style=\'border: 2px solid #ccc\' src=\'" + base_url + folder_image + "product/thumbs/" + strJSON['img_name'] +"\' />");
                       $('#gray').fadeOut("fast",function(){
                            $('#image_lb').hide();
                       });                     
                   }         
            });
        });
        
        $('#productjcropedit').live('click', function(e){
            e.preventDefault();
            
            var x   = jQuery('#x').attr('value');
            var y   = jQuery('#y').attr('value');
            var x2  = jQuery('#x2').attr('value');
            var y2  = jQuery('#y2').attr('value');
            var w   = jQuery('#w').attr('value');
            var h   = jQuery('#h').attr('value');
            var filename   = jQuery('#filename').attr('value');
            //var imagepath = jQuery('#imagepath').attr('value');
            var filepath = jQuery('#filepath').attr('value');
            var link = base_url + 'backend/product/processimg';
            
            $.ajax({
                   url: link,
                   type: "POST",
                   data: "x1="+x+"&y1="+y+"&x2="+x2+"&y2="+y2+"&w="+w+"&h="+h+"&filepath="+filepath,
                   success: function(result){
                       var strJSON = $.parseJSON(result);
                       
                       var value_thumbs = folder_image + 'product/thumbs/' + strJSON['img_name'];
                       var value_image = folder_image + 'product/' + filename;
                       
                       $('#product_thumb_edit').attr('value',value_thumbs);
                       $('#product_img_edit').attr('value',value_image);
                       
                       $('#productthumbsedit').empty();
                       $('#productthumbsedit').html("<img style=\'border: 2px solid #ccc\' src=\'" + base_url + folder_image + "product/thumbs/" + strJSON['img_name'] +"\' />");
                       $('#gray').fadeOut("fast",function(){
                            $('#image_lb').hide();
                       });                     
                   }         
            });
        });
    
    /*Event Cancel*/
    $('#productcancel').live('click', function(e){
        e.preventDefault();
        
        var filepath = jQuery('#filepath').attr('value');
        var link = base_url + 'backend/product/rejectimg';
        
        $.ajax({
            url:   link,
            type:  "POST",
            data:  "filepath="+filepath,
            success: function(result){
                 $('#gray').fadeOut("fast",function(){
                    $('#image_lb').hide();
                window.location.reload();      
                }); 
            }
        });
    });
    
    /****************************UPDATE ORDER*******************************************/
    $(".updatecart").live('click', function(eve){
        
        eve.preventDefault();
        
        var order_status = $(this).parent().parent().find('select[name=order_status]').val();
        var order_id     = $(this).parent().parent().find('input[name=order_id]').val();
        
        //alert('Clicked!');
        
        $.ajax({
           url: base_url + 'backend/cart/updateorder',
           type: "POST",
           data: "order_id="+order_id+"&order_status="+order_status,
           success: function(result){
                
                var strJSON = $.parseJSON(result);
                
                if(strJSON['error'] == 1)
                {
                    $("#ordermsg").show("slow");
                    $("#ordermsg li.msg").text('Có lỗi trong quá trình cập nhật. Vui lòng thử lại.')
                }
                else
                {
                    $("#ordermsg").show("slow");
                    $("#ordermsg li.msg").text('Cập nhật đơn hàng thành công.')
                }
           }
        });
        
        return false;
    });
    
    $("#updateship").live('click', function(eve){
        
        eve.preventDefault();
        
        var order_ship = $(this).parent().parent().find('input[name=ordership]').val();
        var order_id     = $(this).parent().parent().find('input[name=order_id]').val();
        
        //alert('Clicked!');
        
        $.ajax({
           url: base_url + 'backend/cart/updateship',
           type: "POST",
           data: "order_id="+order_id+"&order_ship="+order_ship,
           success: function(result){
                
                var strJSON = $.parseJSON(result);
                
                if(strJSON['error'] == 1)
                {
                    $("#orderup").show("slow");
                    $("#orderup li.msg").text('Có lỗi trong quá trình cập nhật. Vui lòng thử lại.')
                }
                else
                {
                    window.location.reload();
                }
           }
        });
        
        return false;
    });
    
    
    /****************************NEED CHECK**********************************************/
    
    $('.delete').click(function(){
        if(!confirm('Bạn có chắc chắn muốn xoá'))
        {
            return false;
        }  
    })
    
    /****************************CHECK BOX**********************************************/
    //var selected_s = {'checkbox[]' : []};
    
    
    
    $('#dels').live('click', function(){
        
        var selected_s = new Array();
        $("input[name='cus[]']:checked").each(function(){
            
            selected_s.push($(this).val());
            
        })
    
       
        //alert('Clicked.');
       
        $.ajax({
           
           url: base_url + 'backend/customer/dels',
           type: "POST",
           data: "customer_ids="+selected_s,
           success: function(result){
                if(result == 1)
                    window.location.reload();
           }
            
        });
       
        return false; 
    });
});/*****************************End myjquery script************************************/
